﻿using System;
using System.Linq;
using System.Net.Http;
using System.Reactive.Linq;
using System.Threading.Tasks;
using System.Net.Http.Json;
using System.IO;
using System.Collections.Generic;
using MySqlConnector;
using Dapper;
using XSC.Core.Mapper;
using XSC.Module.Lucene;

namespace ConsoleApp1
{
    class Program
    {
        static HttpClient _httpClient = new HttpClient();
        static MySqlConnection con;
        static async Task Main(string[] args)
        {
            //var method =  SMapper.GetMapDicMethod(typeof(string), typeof(int), typeof(int));
            //var method1 = SMapper.GetMapValueMethod(typeof(string));
            //var method2 = SMapper.GetMapListMethod(typeof(int), typeof(int));
            //var method3 = SMapper.GetMapValueListMethod(typeof(int));
            //var method4 = SMapper.GetMapArrayMethod(typeof(int), typeof(int));
            var cccc = 10;
            var a = cccc.CompareTo(1);
            var b = cccc.CompareTo(10);
            var c = cccc.CompareTo(11);
            var obj1 = new Test1() 
            { 
                a = 1,
                b = 2,
                c = 3,
                d = "4",
                f = DateTime.Now,
                g = XX.b,
                Dic_a = new Dictionary<string, int>() { { "a", 1} },
                Dic_b= new Dictionary<string, Test_s>() { { "a", new Test_s() { a = 2 } } },
                list_1 = new List<int>() {  1, 2, 3 },
                list_2 = new List<Test_s>() { new Test_s() { a = 2 } },
                array_1 = new int[] { 1, 2},
                array_2 = new Test_s[] { new Test_s() { a = 2 } },
                cl1 = new Test_s() { a = 1 }
            };
            var obj2 = SMapper.Map<Test1, Test2>(obj1);
            var lu = new LuceneHelper("e:\\searchdata");
            

            Console.WriteLine(obj2.d);
        }
       

        static void x1()
        {
            var lines = File.ReadAllLines("./files/1.txt");
            var csv = File.OpenWrite("./files/1.csv");
            string bms_system = "Server=localhost;Port=61000;Initial Catalog=bms_system;Uid=root;Pwd=!(/Qqi+9ka0y;Allow User Variables=true";
            con = new MySqlConnection(bms_system);

            var codelist = new List<string>();
            foreach (var item in lines)
            {
                codelist.Add(item);
                if (codelist.Count >= 1000)
                {
                    var rsDic = GetBizNos(codelist);
                    foreach (var k in codelist)
                    {
                        var v = rsDic.ContainsKey(k) ? rsDic[k] : "";
                        Console.WriteLine($"{k},{v}");
                    }
                    codelist = new List<string>();
                }
            }
        }
        
        static void ad()
        {
            //_httpClient.BaseAddress = new Uri("http://localhost:10003/");
            _httpClient.BaseAddress = new Uri("https://waas.50yc.com/50bms/sf-openapi/");
            _httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
            var lines = File.ReadAllLines("./files/1.txt");
            Console.WriteLine(lines.Length);

            lines.ToObservable().Window(10).Subscribe(x => x.ToList().Subscribe(
                x =>
                {
                    Post(x.ToList());
                }));

        }
        static void Post(List<string> dats)
        {
            try
            {
                var rs = (_httpClient.PostAsJsonAsync("test/TestSyncZeroByCodes", dats).ConfigureAwait(false).GetAwaiter().GetResult());
                var rsStr = (rs.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult());
                Console.WriteLine(rsStr);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
        static void PostTestsyncpredatabyids(List<string> dats)
        {
            try
            {
                _httpClient.BaseAddress = new Uri("https://waas.50yc.com/50bms/sf-openapi/");
                _httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
                var rs = (_httpClient.PostAsJsonAsync("test/testsyncpredatabyids", dats).ConfigureAwait(false).GetAwaiter().GetResult());
                var rsStr = (rs.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult());
                Console.WriteLine(rsStr);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
        static Dictionary<string,string> GetBizNos(List<string> codes)
        {
            var dic = new Dictionary<string, string>();
            var sql = "select * from open_api_callback_log where Code in @codes";
            var rs = con.Query<ApiLog>(sql, new { codes });
            foreach (var item in rs)
            {
                dic[item.Code] = item.BizNo;
            }
            return dic;
        }

        static void TestGen()
        {
            //var xx = SMapper.GetMapper<Test1, Test2>();
        }
    }

    public class ApiLog
    {
        public string Code { get; set; }
        public string BizNo { get; set; }
    }

    public class Test1
    {
        public int a { get; set; }
        public long b { get; set; }
        public double c { get; set; }
        public string d { get; set; }
        public DateTime f { get; set; }
        public XX g { get; set; }
        public Dictionary<string,int> Dic_a { get; set; }
        public Dictionary<string, Test_s> Dic_b { get; set; }
        public List<int> list_1 { get; set; }
        public List<Test_s> list_2 { get; set; }
        public int[] array_1 { get; set; }
        public Test_s[] array_2 { get; set; }
        public Test_s cl1 { get; set; }
    }
    public enum XX
    {
        a,
        b,
        c
    }
    public class Test2
    {
        public int a { get; set; }
        public long b { get; set; }
        public double c { get; set; }
        public string d { get; set; }
        public DateTime f { get; set; }
        public XX g { get; set; }
        public Dictionary<string, int> Dic_a { get; set; }
        public Dictionary<string, Test_t> Dic_b { get; set; }
        public List<int> list_1 { get; set; }
        public List<Test_t> list_2 { get; set; }
        public int[] array_1 { get; set; }
        public Test_t[] array_2 { get; set; }
        public Test_t cl1 { get; set; }
    }
    public class Test_s
    {
        public int a { get; set; }
    }
    public class Test_t
    {
        public int a { get; set; }
    }
}
